package com.chh.client;

import com.baidu.bjf.remoting.protobuf.Any;
import com.baidu.bjf.remoting.protobuf.Codec;
import com.baidu.bjf.remoting.protobuf.ProtobufProxy;
import com.chh.jproto.HouseJProto;
import com.chh.jproto.PeopleJProto;
import com.chh.util.FileUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 一碗情深
 * @since 2024-08-09 星期五 14:38:02
 */
public class GenerateFile {
    public static void main(String[] args) throws IOException {

        // 时间
        long time = System.currentTimeMillis();

        // 获取用户信息
        PeopleJProto owner = new PeopleJProto("4212434335", "小明", true, 40000.0);
        // 合伙人信息
        Any ownerExtent = Any.pack(new PeopleJProto("534243242", "小李", false, 80000.0));

        // house信息
        List<PeopleJProto> owners = new ArrayList<>();
        owners.add(owner);
        HouseJProto house = new HouseJProto(1, HouseJProto.HouseType.OTHERS, (int) time, 2000000L, owners, ownerExtent);

        // jprotobuf
        try {
            // 序列化
            Codec<HouseJProto> simpleTypeCodec = ProtobufProxy.create(HouseJProto.class);
            byte[] b = simpleTypeCodec.encode(house);
            FileUtils.writeFileByte(b, "JProtobuf");
            System.out.println("JProtobuf序列化后的数据：" + Arrays.toString(b) + ",字节个数：" + b.length);
            // 反序列化
            byte[] bytes = FileUtils.readFileByte("JProtobuf");
            HouseJProto newStt = simpleTypeCodec.decode(bytes);
            System.out.println("JProtobuf反序列化后的数据：" + newStt);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
